×
☰ See All Chapters

Spring @Conditional Annotation

Spring 4.0 introduced the @Conditional annotation to inject the beans conditionally. If condition matches then spring inject the beans otherwise ignores. Earlier we have learnt @Profile annotation from spring 3 where we had support to inject the beans which we need by setting active profile. If we had two profiles and by activating one profile, spring bootstrap the beans of activated profile ignoring other profile beans. So, now what if we still need some beans of inactivated profile or ignore some beans from activated profile, we have to create completely new profile with beans which we need to be bootstrapped. This we can do if we are dealing with multiple beans, but if we just have to ignore or consider one or two beans, creating new profile for those one or two bean is not optimized solution.

Spring 4.0 has now support to consider or ignore single bean by using @Conditional annotation. This @Conditional annotation has attribute which accepts object of type org.springframework.context.annotation.Condition.  

Condition Interface

You should implement Condition interface and provide implementation for matches() method.  matches()should return true or false representing whether condition is satisfied or not. Implementation class object is then supplied to @Conditional annotation.

public class LinuxCondition implements Condition{

        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

                return context.getEnvironment().getProperty("os.name").contains("Linux");

        }

}

Now let us see an example for Conditional Annotation. In our example we inject the beans based on operating system. We wrote Condition implementation classes and we are checking the operating system name in matches() methods.

Spring Conditional Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring4.0_Conditional_Annotation</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Linux.java

package com.java4coding.os;

 

public class Linux {

        public void displayOS(){

                System.out.println("Linux OS");

        }

}

Windows.java

package com.java4coding.os;

 

public class Windows {

        public void displayOS(){

                System.out.println("Windows OS");

        }

}

LinuxCondition.java

package com.java4coding.os.conditions;

 

import org.springframework.context.annotation.Condition;

import org.springframework.context.annotation.ConditionContext;

import org.springframework.core.type.AnnotatedTypeMetadata;

 

public class LinuxCondition implements Condition{

        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

                return context.getEnvironment().getProperty("os.name").contains("Linux");

        }

}

WindowsCondition.java

package com.java4coding.os.conditions;

 

import org.springframework.context.annotation.Condition;

import org.springframework.context.annotation.ConditionContext;

import org.springframework.core.type.AnnotatedTypeMetadata;

 

public class WindowsCondition implements Condition{

        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

                return context.getEnvironment().getProperty("os.name").contains("Windows");

        }

}

SpringConfiguration.java

package com.manum.hassan;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Conditional;

import org.springframework.context.annotation.Configuration;

import com.java4coding.os.Linux;

import com.java4coding.os.Windows;

import com.java4coding.os.conditions.LinuxCondition;

import com.java4coding.os.conditions.WindowsCondition;;

 

@Configuration

public class SpringConfiguration {

        @Bean(name = "linuxBean")

        @Conditional(LinuxCondition.class)

        public Linux linuxOSService() {

                return new Linux();

        }

 

        @Bean(name = "windowsBean")

        @Conditional(WindowsCondition.class)

        public Windows windowsOSService() {

                return new Windows();

        }

}

Demo.java

package com.manum.hassan;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

import com.java4coding.os.Linux;

import com.java4coding.os.Windows;

 

public class Demo {

        public static void main(String[] args) {

               

                ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);

                Linux linux = null;

                Windows windows = null;

                if(context.containsBean("linuxBean")){

                        linux=(Linux) context.getBean("linuxBean");

                }

                if(context.containsBean("windowsBean")){

                        windows=(Windows) context.getBean("windowsBean");

                }

               

                System.out.println("Linux reference: "+linux);

                System.out.println("Windows reference: "+windows);

               

                ((ConfigurableApplicationContext)context).close();

        }

}

 

Output:

spring-conditional-annotation-0
 

All Chapters
Author